﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using System.IO;

namespace Wjtour.Web.Admin.scene
{
    public partial class pictureaddedi : BasePage
    {
        protected TextBox scenename;
        protected HiddenField sceneid;
        protected TextBox img;
        protected HiddenField pictureid;
        protected FileUpload upload;
        protected Button btnPublish;
        protected Image imgPreview;
        protected Button submit;
        protected string type;
        protected TextBox title;
        protected TextBox picturedesc;

        protected int id = 0;
        Wjtour.Domain.scenepicture scenepicture = new Domain.scenepicture();
        Wjtour.Domain.scene scene = new Domain.scene();
        Wjtour.Domain.sitepicture picture = new sitepicture();
        scenepicture_bll _bll = new scenepicture_bll();
        scene_bll _sbll = new scene_bll();
        sitepicture_bll _pbll = new sitepicture_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            type = Request["type"];
            submit.Click += new EventHandler(submit_Click);
            btnPublish.Click += btnPublish_Click;
            if (id > 0)
            {
                scenepicture = _bll.Load(id);
                scene = _sbll.Load(scenepicture.sceneid);
                picture = _pbll.Load(scenepicture.pictureid);
                if (!IsPostBack)
                {
                    if (scenepicture != null)
                    {
                        scenename.Text = scene.scenename;
                        sceneid.Value = scenepicture.sceneid.ToString();
                        pictureid.Value = scenepicture.pictureid.ToString();
                        img.Text = picture.picturepath;
                        imgPreview.ImageUrl = picture.picturepath;
                        title.Text = picture.picturetitle;
                        picturedesc.Text = picture.picturedesc;
                    }
                }
            }
        }

        void btnPublish_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(upload.FileName))
            {
                Utils.Alert(this.Page, "请选择要上传的文件！");
                return;
            }
            string fileExt = Path.GetExtension(upload.FileName);
            List<string> allowExt = new List<string>();
            allowExt.Add(".jpg");
            allowExt.Add(".png");
            allowExt.Add(".gif");

            if (!allowExt.Contains(fileExt))
            {
                Utils.Alert(this.Page, "系统只允许上传jpg,jpeg,gif的文件！");
                return;
            }
            string uploadDir = Server.MapPath("/uploads");
            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            string guid = GetFileName("time");
            string filename = guid + fileExt;
            string path = uploadDir + "\\" + filename;
            if (System.IO.File.Exists(filename)) System.IO.File.Delete(path);

            try
            {
                upload.SaveAs(path);
                //string newpath = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename, 100, 100, "jpg");
                imgPreview.ImageUrl = "/uploads/" + filename;
                img.Text = "/uploads/" + filename;

                Wjtour.Domain.sitepicture sp = new sitepicture();
                sp.picturetitle = title.Text;
                sp.picturetypeid = 6;
                sp.picturepath = img.Text;
                sp.picturedesc = picturedesc.Text;
                sp.createdate = DateTime.Now;
                int spid = 0;
                bool b = _pbll.Save(sp,out spid);
                pictureid.Value = spid.ToString();
            }
            catch (Exception ex)
            {
                Utils.Alert(this.Page, "文件文件出错，文件大小超过限制或网络原因！");
                return;
            }
        }

        void submit_Click(object sender, EventArgs e)
        {
            scenepicture.id = id;
            scenepicture.sceneid = Utils.StrToInt(sceneid.Value, 0);
            scenepicture.pictureid = Utils.StrToInt(pictureid.Value, 0);
            if (scenepicture.id == 0)
                scenepicture.addtime = DateTime.Now;

            bool b = _bll.Save(scenepicture);
            if (b)
            {
                Wjtour.Domain.sitepicture sp = _pbll.Load(scenepicture.pictureid);
                if (sp != null)
                {
                    sp.picturetitle = title.Text;
                    sp.picturedesc = picturedesc.Text;
                    _pbll.Save(sp);
                }

                SaveLog(id == 0 ? "Add" : "edit");
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "picturelist.aspx?type=" + type, false);
            }
        }
    }
}